{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tushare as ts\n",
    "import matplotlib.pyplot as plt\n",
    "import pandas as pd\n",
    "import warnings\n",
    "import sys\n",
    "import datetime\n",
    "\n",
    "sys.path.append(\"..\")\n",
    "\n",
    "from imp import reload\n",
    "\n",
    "import common\n",
    "reload(common)\n",
    "reload(common.analysis_helper)\n",
    "\n",
    "warnings.filterwarnings('ignore')"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "交易所代码 CFFEX-中金所 DCE-大商所 CZCE-郑商所 SHFE-上期所 INE-上海国际能源交易中心"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "config = config_helper()\n",
    "start_date = \"20200101\"\n",
    "end_date = \"20221231\"\n",
    "pro = ts.pro_api(config.tushare_token)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ts_code</th>\n",
       "      <th>trade_date</th>\n",
       "      <th>pre_close</th>\n",
       "      <th>pre_settle</th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>settle</th>\n",
       "      <th>change1</th>\n",
       "      <th>change2</th>\n",
       "      <th>vol</th>\n",
       "      <th>amount</th>\n",
       "      <th>oi</th>\n",
       "      <th>oi_chg</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: [ts_code, trade_date, pre_close, pre_settle, open, high, low, close, settle, change1, change2, vol, amount, oi, oi_chg]\n",
       "Index: []"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "start_date = \"20100101\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ts_code</th>\n",
       "      <th>trade_date</th>\n",
       "      <th>pre_close</th>\n",
       "      <th>pre_settle</th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>settle</th>\n",
       "      <th>change1</th>\n",
       "      <th>change2</th>\n",
       "      <th>vol</th>\n",
       "      <th>amount</th>\n",
       "      <th>oi</th>\n",
       "      <th>oi_chg</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>CU1811.SHF</td>\n",
       "      <td>20181113</td>\n",
       "      <td>48900.0</td>\n",
       "      <td>49030.0</td>\n",
       "      <td>48910.0</td>\n",
       "      <td>49040.0</td>\n",
       "      <td>48700.0</td>\n",
       "      <td>49030.0</td>\n",
       "      <td>48830.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-200.0</td>\n",
       "      <td>17270.0</td>\n",
       "      <td>421721.70</td>\n",
       "      <td>16110.0</td>\n",
       "      <td>-6830.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>CU1811.SHF</td>\n",
       "      <td>20181112</td>\n",
       "      <td>49270.0</td>\n",
       "      <td>49340.0</td>\n",
       "      <td>49130.0</td>\n",
       "      <td>49200.0</td>\n",
       "      <td>48860.0</td>\n",
       "      <td>48900.0</td>\n",
       "      <td>49030.0</td>\n",
       "      <td>-440.0</td>\n",
       "      <td>-310.0</td>\n",
       "      <td>27710.0</td>\n",
       "      <td>679447.85</td>\n",
       "      <td>22940.0</td>\n",
       "      <td>-7160.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>CU1811.SHF</td>\n",
       "      <td>20181109</td>\n",
       "      <td>49440.0</td>\n",
       "      <td>49500.0</td>\n",
       "      <td>49340.0</td>\n",
       "      <td>49530.0</td>\n",
       "      <td>49120.0</td>\n",
       "      <td>49270.0</td>\n",
       "      <td>49340.0</td>\n",
       "      <td>-230.0</td>\n",
       "      <td>-160.0</td>\n",
       "      <td>22530.0</td>\n",
       "      <td>555910.15</td>\n",
       "      <td>30100.0</td>\n",
       "      <td>-4700.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>CU1811.SHF</td>\n",
       "      <td>20181108</td>\n",
       "      <td>49470.0</td>\n",
       "      <td>49460.0</td>\n",
       "      <td>49600.0</td>\n",
       "      <td>49680.0</td>\n",
       "      <td>49350.0</td>\n",
       "      <td>49440.0</td>\n",
       "      <td>49500.0</td>\n",
       "      <td>-20.0</td>\n",
       "      <td>40.0</td>\n",
       "      <td>22290.0</td>\n",
       "      <td>551708.00</td>\n",
       "      <td>34800.0</td>\n",
       "      <td>-3530.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>CU1811.SHF</td>\n",
       "      <td>20181107</td>\n",
       "      <td>49670.0</td>\n",
       "      <td>49630.0</td>\n",
       "      <td>49640.0</td>\n",
       "      <td>49850.0</td>\n",
       "      <td>49260.0</td>\n",
       "      <td>49470.0</td>\n",
       "      <td>49460.0</td>\n",
       "      <td>-160.0</td>\n",
       "      <td>-170.0</td>\n",
       "      <td>26850.0</td>\n",
       "      <td>664040.10</td>\n",
       "      <td>38330.0</td>\n",
       "      <td>-4560.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>205</th>\n",
       "      <td>CU1811.SHF</td>\n",
       "      <td>20180108</td>\n",
       "      <td>56200.0</td>\n",
       "      <td>56300.0</td>\n",
       "      <td>56010.0</td>\n",
       "      <td>56070.0</td>\n",
       "      <td>55750.0</td>\n",
       "      <td>55910.0</td>\n",
       "      <td>55840.0</td>\n",
       "      <td>-390.0</td>\n",
       "      <td>-460.0</td>\n",
       "      <td>84.0</td>\n",
       "      <td>2345.53</td>\n",
       "      <td>1412.0</td>\n",
       "      <td>48.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>206</th>\n",
       "      <td>CU1811.SHF</td>\n",
       "      <td>20180105</td>\n",
       "      <td>56650.0</td>\n",
       "      <td>56590.0</td>\n",
       "      <td>56600.0</td>\n",
       "      <td>56600.0</td>\n",
       "      <td>56090.0</td>\n",
       "      <td>56200.0</td>\n",
       "      <td>56300.0</td>\n",
       "      <td>-390.0</td>\n",
       "      <td>-290.0</td>\n",
       "      <td>46.0</td>\n",
       "      <td>1295.05</td>\n",
       "      <td>1364.0</td>\n",
       "      <td>20.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>207</th>\n",
       "      <td>CU1811.SHF</td>\n",
       "      <td>20180104</td>\n",
       "      <td>56200.0</td>\n",
       "      <td>56210.0</td>\n",
       "      <td>56110.0</td>\n",
       "      <td>56660.0</td>\n",
       "      <td>56070.0</td>\n",
       "      <td>56650.0</td>\n",
       "      <td>56590.0</td>\n",
       "      <td>440.0</td>\n",
       "      <td>380.0</td>\n",
       "      <td>44.0</td>\n",
       "      <td>1245.12</td>\n",
       "      <td>1344.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>208</th>\n",
       "      <td>CU1811.SHF</td>\n",
       "      <td>20180103</td>\n",
       "      <td>56610.0</td>\n",
       "      <td>56490.0</td>\n",
       "      <td>56540.0</td>\n",
       "      <td>56580.0</td>\n",
       "      <td>56110.0</td>\n",
       "      <td>56200.0</td>\n",
       "      <td>56210.0</td>\n",
       "      <td>-290.0</td>\n",
       "      <td>-280.0</td>\n",
       "      <td>166.0</td>\n",
       "      <td>4666.23</td>\n",
       "      <td>1344.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>209</th>\n",
       "      <td>CU1811.SHF</td>\n",
       "      <td>20180102</td>\n",
       "      <td>56820.0</td>\n",
       "      <td>57000.0</td>\n",
       "      <td>56870.0</td>\n",
       "      <td>56870.0</td>\n",
       "      <td>56390.0</td>\n",
       "      <td>56610.0</td>\n",
       "      <td>56490.0</td>\n",
       "      <td>-390.0</td>\n",
       "      <td>-510.0</td>\n",
       "      <td>64.0</td>\n",
       "      <td>1807.72</td>\n",
       "      <td>1344.0</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>210 rows × 15 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        ts_code trade_date  pre_close  pre_settle     open     high      low  \\\n",
       "0    CU1811.SHF   20181113    48900.0     49030.0  48910.0  49040.0  48700.0   \n",
       "1    CU1811.SHF   20181112    49270.0     49340.0  49130.0  49200.0  48860.0   \n",
       "2    CU1811.SHF   20181109    49440.0     49500.0  49340.0  49530.0  49120.0   \n",
       "3    CU1811.SHF   20181108    49470.0     49460.0  49600.0  49680.0  49350.0   \n",
       "4    CU1811.SHF   20181107    49670.0     49630.0  49640.0  49850.0  49260.0   \n",
       "..          ...        ...        ...         ...      ...      ...      ...   \n",
       "205  CU1811.SHF   20180108    56200.0     56300.0  56010.0  56070.0  55750.0   \n",
       "206  CU1811.SHF   20180105    56650.0     56590.0  56600.0  56600.0  56090.0   \n",
       "207  CU1811.SHF   20180104    56200.0     56210.0  56110.0  56660.0  56070.0   \n",
       "208  CU1811.SHF   20180103    56610.0     56490.0  56540.0  56580.0  56110.0   \n",
       "209  CU1811.SHF   20180102    56820.0     57000.0  56870.0  56870.0  56390.0   \n",
       "\n",
       "       close   settle  change1  change2      vol     amount       oi  oi_chg  \n",
       "0    49030.0  48830.0      0.0   -200.0  17270.0  421721.70  16110.0 -6830.0  \n",
       "1    48900.0  49030.0   -440.0   -310.0  27710.0  679447.85  22940.0 -7160.0  \n",
       "2    49270.0  49340.0   -230.0   -160.0  22530.0  555910.15  30100.0 -4700.0  \n",
       "3    49440.0  49500.0    -20.0     40.0  22290.0  551708.00  34800.0 -3530.0  \n",
       "4    49470.0  49460.0   -160.0   -170.0  26850.0  664040.10  38330.0 -4560.0  \n",
       "..       ...      ...      ...      ...      ...        ...      ...     ...  \n",
       "205  55910.0  55840.0   -390.0   -460.0     84.0    2345.53   1412.0    48.0  \n",
       "206  56200.0  56300.0   -390.0   -290.0     46.0    1295.05   1364.0    20.0  \n",
       "207  56650.0  56590.0    440.0    380.0     44.0    1245.12   1344.0     0.0  \n",
       "208  56200.0  56210.0   -290.0   -280.0    166.0    4666.23   1344.0     0.0  \n",
       "209  56610.0  56490.0   -390.0   -510.0     64.0    1807.72   1344.0     2.0  \n",
       "\n",
       "[210 rows x 15 columns]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pro.fut_daily(ts_code='CU1811.SHF', start_date='20180101', end_date='20181113')\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ts_code</th>\n",
       "      <th>symbol</th>\n",
       "      <th>name</th>\n",
       "      <th>list_date</th>\n",
       "      <th>delist_date</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>NI1612.SHF</td>\n",
       "      <td>NI1612</td>\n",
       "      <td>沪镍1612</td>\n",
       "      <td>20151216</td>\n",
       "      <td>20161215</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>AL1405.SHF</td>\n",
       "      <td>AL1405</td>\n",
       "      <td>沪铝1405</td>\n",
       "      <td>20130516</td>\n",
       "      <td>20140515</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>HC1602.SHF</td>\n",
       "      <td>HC1602</td>\n",
       "      <td>热轧卷板1602</td>\n",
       "      <td>20150216</td>\n",
       "      <td>20160215</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>BU1511.SHF</td>\n",
       "      <td>BU1511</td>\n",
       "      <td>沥青1511</td>\n",
       "      <td>20150518</td>\n",
       "      <td>20151116</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>RU9804.SHF</td>\n",
       "      <td>RU9804</td>\n",
       "      <td>橡胶9804</td>\n",
       "      <td>19971113</td>\n",
       "      <td>19980415</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2596</th>\n",
       "      <td>RU0206.SHF</td>\n",
       "      <td>RU0206</td>\n",
       "      <td>橡胶0206</td>\n",
       "      <td>20020304</td>\n",
       "      <td>20020617</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2597</th>\n",
       "      <td>AL1209.SHF</td>\n",
       "      <td>AL1209</td>\n",
       "      <td>沪铝1209</td>\n",
       "      <td>20110916</td>\n",
       "      <td>20120917</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2598</th>\n",
       "      <td>WR1611.SHF</td>\n",
       "      <td>WR1611</td>\n",
       "      <td>线材1611</td>\n",
       "      <td>20151117</td>\n",
       "      <td>20161115</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2599</th>\n",
       "      <td>RU0610.SHF</td>\n",
       "      <td>RU0610</td>\n",
       "      <td>橡胶0610</td>\n",
       "      <td>20051018</td>\n",
       "      <td>20061016</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2600</th>\n",
       "      <td>RB1101.SHF</td>\n",
       "      <td>RB1101</td>\n",
       "      <td>螺纹钢1101</td>\n",
       "      <td>20100118</td>\n",
       "      <td>20110117</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2601 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "         ts_code  symbol      name list_date delist_date\n",
       "0     NI1612.SHF  NI1612    沪镍1612  20151216    20161215\n",
       "1     AL1405.SHF  AL1405    沪铝1405  20130516    20140515\n",
       "2     HC1602.SHF  HC1602  热轧卷板1602  20150216    20160215\n",
       "3     BU1511.SHF  BU1511    沥青1511  20150518    20151116\n",
       "4     RU9804.SHF  RU9804    橡胶9804  19971113    19980415\n",
       "...          ...     ...       ...       ...         ...\n",
       "2596  RU0206.SHF  RU0206    橡胶0206  20020304    20020617\n",
       "2597  AL1209.SHF  AL1209    沪铝1209  20110916    20120917\n",
       "2598  WR1611.SHF  WR1611    线材1611  20151117    20161115\n",
       "2599  RU0610.SHF  RU0610    橡胶0610  20051018    20061016\n",
       "2600  RB1101.SHF  RB1101   螺纹钢1101  20100118    20110117\n",
       "\n",
       "[2601 rows x 5 columns]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "df = pro.fut_basic(exchange='SHFE', fut_type='1', fields='ts_code,symbol,name,list_date,delist_date')\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ts_code</th>\n",
       "      <th>symbol</th>\n",
       "      <th>name</th>\n",
       "      <th>list_date</th>\n",
       "      <th>delist_date</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2429</th>\n",
       "      <td>FU2304.SHF</td>\n",
       "      <td>FU2304</td>\n",
       "      <td>燃油2304</td>\n",
       "      <td>20220401</td>\n",
       "      <td>20230331</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1095</th>\n",
       "      <td>RB2303.SHF</td>\n",
       "      <td>RB2303</td>\n",
       "      <td>螺纹钢2303</td>\n",
       "      <td>20220316</td>\n",
       "      <td>20230315</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>727</th>\n",
       "      <td>BU2403.SHF</td>\n",
       "      <td>BU2403</td>\n",
       "      <td>沥青2403</td>\n",
       "      <td>20220316</td>\n",
       "      <td>20240315</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1489</th>\n",
       "      <td>CU2303.SHF</td>\n",
       "      <td>CU2303</td>\n",
       "      <td>沪铜2303</td>\n",
       "      <td>20220316</td>\n",
       "      <td>20230315</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>HC2303.SHF</td>\n",
       "      <td>HC2303</td>\n",
       "      <td>热轧卷板2303</td>\n",
       "      <td>20220316</td>\n",
       "      <td>20230315</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>218</th>\n",
       "      <td>SS2303.SHF</td>\n",
       "      <td>SS2303</td>\n",
       "      <td>不锈钢2303</td>\n",
       "      <td>20220316</td>\n",
       "      <td>20230315</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>666</th>\n",
       "      <td>WR2303.SHF</td>\n",
       "      <td>WR2303</td>\n",
       "      <td>线材2303</td>\n",
       "      <td>20220316</td>\n",
       "      <td>20230315</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>830</th>\n",
       "      <td>NI2303.SHF</td>\n",
       "      <td>NI2303</td>\n",
       "      <td>沪镍2303</td>\n",
       "      <td>20220316</td>\n",
       "      <td>20230315</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>187</th>\n",
       "      <td>PB2303.SHF</td>\n",
       "      <td>PB2303</td>\n",
       "      <td>沪铅2303</td>\n",
       "      <td>20220316</td>\n",
       "      <td>20230315</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>58</th>\n",
       "      <td>AG2303.SHF</td>\n",
       "      <td>AG2303</td>\n",
       "      <td>沪银2303</td>\n",
       "      <td>20220316</td>\n",
       "      <td>20230315</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1482</th>\n",
       "      <td>AL2303.SHF</td>\n",
       "      <td>AL2303</td>\n",
       "      <td>沪铝2303</td>\n",
       "      <td>20220316</td>\n",
       "      <td>20230315</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2324</th>\n",
       "      <td>RU2303.SHF</td>\n",
       "      <td>RU2303</td>\n",
       "      <td>橡胶2303</td>\n",
       "      <td>20220316</td>\n",
       "      <td>20230315</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1160</th>\n",
       "      <td>AU2304.SHF</td>\n",
       "      <td>AU2304</td>\n",
       "      <td>沪金2304</td>\n",
       "      <td>20220316</td>\n",
       "      <td>20230417</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2144</th>\n",
       "      <td>ZN2303.SHF</td>\n",
       "      <td>ZN2303</td>\n",
       "      <td>沪锌2303</td>\n",
       "      <td>20220316</td>\n",
       "      <td>20230315</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1180</th>\n",
       "      <td>SP2303.SHF</td>\n",
       "      <td>SP2303</td>\n",
       "      <td>纸浆2303</td>\n",
       "      <td>20220316</td>\n",
       "      <td>20230315</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2141</th>\n",
       "      <td>BU2302.SHF</td>\n",
       "      <td>BU2302</td>\n",
       "      <td>沥青2302</td>\n",
       "      <td>20220316</td>\n",
       "      <td>20230215</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2140</th>\n",
       "      <td>BU2301.SHF</td>\n",
       "      <td>BU2301</td>\n",
       "      <td>沥青2301</td>\n",
       "      <td>20220316</td>\n",
       "      <td>20230116</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2139</th>\n",
       "      <td>BU2211.SHF</td>\n",
       "      <td>BU2211</td>\n",
       "      <td>沥青2211</td>\n",
       "      <td>20220316</td>\n",
       "      <td>20221115</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2138</th>\n",
       "      <td>BU2210.SHF</td>\n",
       "      <td>BU2210</td>\n",
       "      <td>沥青2210</td>\n",
       "      <td>20220316</td>\n",
       "      <td>20221017</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2099</th>\n",
       "      <td>SN2303.SHF</td>\n",
       "      <td>SN2303</td>\n",
       "      <td>沪锡2303</td>\n",
       "      <td>20220316</td>\n",
       "      <td>20230315</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         ts_code  symbol      name list_date delist_date\n",
       "2429  FU2304.SHF  FU2304    燃油2304  20220401    20230331\n",
       "1095  RB2303.SHF  RB2303   螺纹钢2303  20220316    20230315\n",
       "727   BU2403.SHF  BU2403    沥青2403  20220316    20240315\n",
       "1489  CU2303.SHF  CU2303    沪铜2303  20220316    20230315\n",
       "21    HC2303.SHF  HC2303  热轧卷板2303  20220316    20230315\n",
       "218   SS2303.SHF  SS2303   不锈钢2303  20220316    20230315\n",
       "666   WR2303.SHF  WR2303    线材2303  20220316    20230315\n",
       "830   NI2303.SHF  NI2303    沪镍2303  20220316    20230315\n",
       "187   PB2303.SHF  PB2303    沪铅2303  20220316    20230315\n",
       "58    AG2303.SHF  AG2303    沪银2303  20220316    20230315\n",
       "1482  AL2303.SHF  AL2303    沪铝2303  20220316    20230315\n",
       "2324  RU2303.SHF  RU2303    橡胶2303  20220316    20230315\n",
       "1160  AU2304.SHF  AU2304    沪金2304  20220316    20230417\n",
       "2144  ZN2303.SHF  ZN2303    沪锌2303  20220316    20230315\n",
       "1180  SP2303.SHF  SP2303    纸浆2303  20220316    20230315\n",
       "2141  BU2302.SHF  BU2302    沥青2302  20220316    20230215\n",
       "2140  BU2301.SHF  BU2301    沥青2301  20220316    20230116\n",
       "2139  BU2211.SHF  BU2211    沥青2211  20220316    20221115\n",
       "2138  BU2210.SHF  BU2210    沥青2210  20220316    20221017\n",
       "2099  SN2303.SHF  SN2303    沪锡2303  20220316    20230315"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.sort_values(\"list_date\", ascending=False).head(20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ts_code</th>\n",
       "      <th>symbol</th>\n",
       "      <th>name</th>\n",
       "      <th>list_date</th>\n",
       "      <th>delist_date</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1467</th>\n",
       "      <td>RB2205.SHF</td>\n",
       "      <td>RB2205</td>\n",
       "      <td>螺纹钢2205</td>\n",
       "      <td>20210518</td>\n",
       "      <td>20220516</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         ts_code  symbol     name list_date delist_date\n",
       "1467  RB2205.SHF  RB2205  螺纹钢2205  20210518    20220516"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "\n",
    "df_rb = df[df['ts_code'] == \"RB2205.SHF\"]\n",
    "df_rb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "klines = pro.fut_daily(ts_code=\"RB2205.SHF\", start_date = '20200518', end_date = '20220516')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "import talib as ta"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_prediction_data(klines, n):\n",
    "    \"\"\"获取用于随机森林的n个输入数据(n为数据长度): n天中每天的特征参数及其涨跌情况\"\"\"\n",
    "    close_prices = klines.close[- 30 - n:]  # 获取本交易日及以前的收盘价(此时在预定的收盘时间: 认为本交易日已收盘)\n",
    "    # 计算所需指标\n",
    "    sma_data = ta.SMA(close_prices, 30)[-n:]  # SMA指标, 函数默认时间周期参数:30\n",
    "    wma_data = ta.WMA(close_prices, 30)[-n:]  # WMA指标\n",
    "    mom_data = ta.MOM(close_prices, 30)[-n:]  # MOM指标\n",
    "    x_all = list(zip(sma_data, wma_data, mom_data))  # 样本特征组\n",
    "    y_all = list(klines.close.iloc[i] >= klines.close.iloc[i - 1] for i in list(reversed(range(-1, -n - 1, -1))))  # 样本标签组\n",
    "    # x_all:            大前天指标 前天指标 昨天指标 (今天指标)\n",
    "    # y_all:   (大前天)    前天     昨天    今天      -明天-\n",
    "    # 准备算法需要用到的数据\n",
    "    x_train = x_all[: -1]  # 训练数据: 特征\n",
    "    x_predict = x_all[-1]  # 预测数据(用本交易日的指标预测下一交易日的涨跌)\n",
    "    y_train = y_all[1:]  # 训练数据: 标签 (去掉第一个数据后让其与指标隔一位对齐(例如: 昨天的特征 -> 对应预测今天的涨跌标签))\n",
    "\n",
    "    return x_train, y_train, x_predict\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [],
   "source": [
    "predictions=[]\n",
    "\n",
    "x_train, y_train, x_predict = get_prediction_data(klines, 75)  # 参数1: K线, 参数2:需要的数据长度\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4851.4, 4865.189247311828, 380.0)"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_predict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [],
   "source": [
    "clf = RandomForestClassifier(n_estimators=30, bootstrap=True)\n",
    "clf.fit(x_train, y_train)  # 传入训练数据, 进行参数训练\n",
    "predictions.append(bool(clf.predict([x_predict])))  # 传入测试数据进行预测, 得到预测的结果\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[True]"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "predictions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [],
   "source": [
    "predictions=[]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
